package utgame.com.servicedemo;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import utgame.com.basedemo.BaseActivity;

public class ServiceActivity extends BaseActivity implements View.OnClickListener{

    private TwoService service = null;
    private boolean isBind = false;


    private Button mBtnStartService;
    private Button mBtnStopService;


    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            isBind = true;
            TwoService.MyBinder myBinder = (TwoService.MyBinder) binder;
            service = myBinder.getService();
            Log.i("Kathy", "ActivityA - onServiceConnected");
            int num = service.getRandomNumber();
            Log.i("Kathy", "ActivityA - getRandomNumber = " + num);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBind = false;
            Log.i("Kathy", "ActivityA - onServiceDisconnected");
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_service);

        initView();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_start_service){
            //通过Intent绑定MyService,加入输入参数
            Intent intent=new Intent(ServiceActivity.this, OneService.class);
            intent.putExtra("Name", "Leslie");
            Log.i(Context.ACTIVITY_SERVICE, "----------onClick startService-----------");
            //启动MyService
            startService(intent);

        } else if (view.getId() == R.id.btn_stop_service){
            Intent intent=new Intent(ServiceActivity.this, OneService.class);
            Log.i(Context.ACTIVITY_SERVICE, "----------onClick stopService------------");
            //停止MyService
            stopService(intent);
        } else if (view.getId() == R.id.btn_bind_service) {
            //单击了“bindService”按钮
            Intent intent = new Intent(this, TwoService.class);
            intent.putExtra("from", "ActivityA");
            Log.i("Kathy", "----------------------------------------------------------------------");
            Log.i("Kathy", "ActivityA 执行 bindService");
            bindService(intent, conn, BIND_AUTO_CREATE);
        } else if (view.getId() == R.id.btn_unbind_service) {
            //单击了“unbindService”按钮
            if (isBind) {
                Log.i("Kathy",
                        "----------------------------------------------------------------------");
                Log.i("Kathy", "ActivityA 执行 unbindService");
                unbindService(conn);
            }
        } else if (view.getId() == R.id.btn_finish) {
            //单击了“Finish”按钮
            Log.i("Kathy",
                    "----------------------------------------------------------------------");
            Log.i("Kathy", "ActivityA 执行 finish");
            this.finish();
        }

    }

    private void initView(){
        mBtnStartService = (Button)findViewById(R.id.btn_start_service);
        mBtnStartService.setOnClickListener(this);
        mBtnStopService = (Button)findViewById(R.id.btn_stop_service);
        mBtnStopService.setOnClickListener(this);

        findViewById(R.id.btn_bind_service).setOnClickListener(this);
        findViewById(R.id.btn_unbind_service).setOnClickListener(this);

    }
}

